package estates.persist.controller;

import java.util.List;
import estates.model.Estate;
import javax.persistence.*;

/**
 * Controller for our Estate entity
 * @version 1.0
 * @author Marcin Chwedziak
 * @see estates.controller.Controller
 */
public class EstateController extends Controller<Estate> {
    public EstateController() {
        super(Estate.class);
    }

    @Override
    public List<Estate> list(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from Estate as o where (select count(*) from Estate as e where e.element = o.id) = 0");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }
}
